﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Impls;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;
using Microhard.WeixinMP.Models;
using Microhard.Permission.Interface;
using Micrhard.FMCG.Server.Msg;
using Microhard.WeixinMP.Unities;

namespace Micrhard.FMCG.Server.Weixin
{
    public class GetKADisplayPic_TakeKACode_2 : BaseResponseImpl
    {
        private ISubKA sk = IoC.Resolve<ISubKA>();

        public override bool IsCallMe(Microhard.WeixinMP.Models.RequestServiceModel model)
        {
            if (model.Txt != null && model.Txt.Content != null)
            {
                string kaCode = model.Txt.Content.Content;
                //是否是KA中的编码
                return sk.IsKACode(kaCode);
            }
            return false;
        }

        protected override Microhard.WeixinMP.Models.WeixinBaseData<object> CreateFeatureResponse(Microhard.WeixinMP.Models.RequestServiceModel request, Microhard.WeixinMP.Models.WeixinBaseData<object> response)
        {
            response.MsgType = WeixinMsgType.Text;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            WeixinTxt result = new WeixinTxt();

            //验证用户是否合法
            IWeiXinAccount iwa = IoC.Resolve<IWeiXinAccount>();
            string realUserName = string.Empty;
            bool illegal = iwa.ValidateByWeiXinUserName(response.ToUserName, out realUserName);
            if (illegal)
            {
                //是否是用户管理的KA
                if (sk.IsUserManageKA(response.ToUserName, request.Txt.Content.Content))
                {
                    result.Content = KADisplayMsg.SendPic;
                    //在用户命令队列中增加接收陈列图片的命令
                    WeixinUserCommandQueue queue = new WeixinUserCommandQueue();
                    queue.UpdateCache(response.ToUserName, new UserCommand
                    {
                        BusinessValue = request.Txt.Content.Content,
                        Command = KADisplayMsg.TakePicCommand,
                        CreateTime = DateTime.Now,
                        WeixinOpenID = response.ToUserName
                    });
                }
                else
                {
                    result.Content = KADisplayMsg.NotInMngScope;
                }
            }
            else
            {
                result.Content = KADisplayMsg.NoneUser;
            }
            response.Content = result;
            return response;
        }
    }
}
